package com.jrx.anytxn.transaction.job.marketingFile.config;

import com.jrx.anytxn.common.uitl.DateUtils;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.ntfs.ICreateTskdFileSampleSimple;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.transaction.config.InterestFileConfig;
import com.jrx.anytxn.transaction.config.MarketingFileConfig;
import com.jrx.anytxn.transaction.config.SftpConfig;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.File;

@Configuration
@EnableConfigurationProperties({SftpConfig.class, InterestFileConfig.class})
public class MarketingFileUploadConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(MarketingFileUploadConfiguration.class);

    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private MarketingFileConfig marketingFileConfig;
    @Autowired
    private IOrganizationTableService organizationTableService;
    @Autowired
    private ICreateTskdFileSampleSimple createTskdFileSampleSimple;

    @Bean
    public Job marketingFileUploadJob() {
        return jobBuilderFactory.get("marketingFileUploadJob")
                .start(marketingFileUploadStep())
                .build();
    }

    @Bean
    public Step marketingFileUploadStep() {
        return stepBuilderFactory.get("marketingFileUploadStep")
                .tasklet(marketingFileUploadTasklet())
                .build();
    }

    @Bean
    @StepScope
    public Tasklet marketingFileUploadTasklet() {
        return (stepContribution, chunkContext) -> {
            logger.info("营销文件上传开始。。。。。。");

            PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(TransBizConstant.DEFAULT_ORG_ID);
            String today = DateUtils.format(org.getToday(),"yyyyMMdd");

            String basePath = marketingFileConfig.getMarketingFilePath();
            String nftsPath = marketingFileConfig.getNftsPath();
            String outPath = basePath + org.getTenantId() + File.separator + today + File.separator + marketingFileConfig.getOutDir();

            try {
                createTskdFileSampleSimple.initNfsTransFile(outPath, nftsPath, "", "s_anyrcs_mrdzwj" ,org.getTenantId(),today,"0");
            } catch (Exception e1) {
                logger.error("文件传输失败",e1);
                throw new RuntimeException("文件传输失败");
            }

            logger.info("营销文件上传结束。。。。。。");
            return RepeatStatus.FINISHED;
        };
    }
}
